package com.mojo.netty;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 基于socket的服务，单线程处理
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2021, 广州九尾信息科技有限公司
 * @date 2021/8/25 17:45
 */
public class SocketServer01 {
    public static void main(String[] args) throws IOException {
        //Socket socket = new Socket("127.0.0.1", 8080);
        ServerSocket serverSocket = new ServerSocket(8801);
        while (true) {
            try (Socket accept = serverSocket.accept();
                 OutputStream outputStream = accept.getOutputStream();) {
                Thread.sleep(20);
                PrintWriter writer = new PrintWriter(outputStream, true);
                writer.println("HTTP1.1 200 OK");
                writer.println("ContentType:text/html;charset=utf-8");
                writer.println();
                writer.println("hello socket server");
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
